package com.example.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.AttendanceMapper;
import com.example.dao.TimeMapper;
import com.example.dao.UserMapper;
import com.example.pojo.Attendance;
import com.example.pojo.Time;
import com.example.util.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/attendance")
@Api(tags = "考勤管理")
public class AttendanceController{

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TimeMapper timeMapper;

    @RequestMapping("/selectAllInfo")
    public Result selectAll(){  //查询全部打卡的人数
        List<Attendance> attendances = attendanceMapper.selectList(null);
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",attendances);
        return new Result("200","查询成功",map);
    }

    @RequestMapping("/selectSelf")
    public Result selectSelf(Integer userid){  //查询自己的全部打卡记录
        QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", userid);
        List<Attendance> attendances = attendanceMapper.selectList(wrapper);
        return new Result("200","查询成功",attendances);
    }



    @RequestMapping("/selectTime")
    public Result selectTime(Integer userid){  //查询当天是否打卡
        LocalDate today = LocalDate.now(); // 获取当天的时间
        QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
        wrapper.eq("day_time", today).eq("userid",userid);
        Attendance attendances = attendanceMapper.selectOne(wrapper);

        if (attendances!=null){
            if(attendances.getLateTime()!=null){
                return new Result("200","查询成功","已经打卡完毕");
            }
                return new Result("200","查询成功","已打卡");
        }
        return new Result("200","查询成功","未打卡");
    }



    @PostMapping("/insertInfo") // 判断考勤情况，并且加入状态
    public Result insert(@RequestBody Attendance attendance) { // 判断打卡的情况
        LocalDate today = LocalDate.now(); // 获取当天的时间
        Integer id = 1; // 这是默认的打卡的userid

        QueryWrapper<Time> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Time time = timeMapper.selectOne(wrapper); // 当前userid为1的Time对象

        if (time == null) {
            // 如果没有找到记录，可以返回错误信息或进行其他处理
            return new Result("404", "未找到考勤记录", null);
        }

        LocalTime earlyTime = LocalTime.from(time.getEarlyTime()); // 规定早勤时间
        LocalTime lateTime = LocalTime.from(time.getLateTime()); // 规定晚勤时间



        if (attendance != null){  //判断时间不为空

            attendance.setDayTime(today); // 将今天的时间录入

            if(attendance.getEarlyTime()==null&&attendance.getLateTime()==null){  //没有打卡
                attendance.setStatus(5);
                int insert = attendanceMapper.insert(attendance); // 添加
                if (insert > 0) {
                    return new Result("200", "插入成功", insert);
                } else {
                    return new Result("500", "插入失败", null);
                }
            }

            if (attendance.getEarlyTime() != null){   //判断早上的时间
                attendance.setTimes(1);   //打卡次数
                LocalTime time3 = LocalTime.from(attendance.getEarlyTime()); // 你的早勤时间
                if (time3.isAfter(earlyTime) && time3.isBefore(lateTime)){
                    attendance.setStatus(1);  //打卡状态
                } else {
                    attendance.setStatus(0);
                }
                int insert = attendanceMapper.insert(attendance); // 添加
                if (insert > 0) {
                    return new Result("200", "插入成功", insert);
                } else {
                    return new Result("500", "插入失败", null);
                }
            }

            if (attendance.getLateTime() != null) {  // 晚上打卡
                QueryWrapper<Attendance> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("userid", attendance.getUserid()).eq("day_time",today);
                Attendance attendance2 = attendanceMapper.selectOne(wrapper2); // 查出来的对象

                if (attendance2 != null) {
                    // 如果没有找到记录，可以返回错误信息或进行其他处理
//                    return new Result("404", "未找到考勤记录", null);
                    attendance.setTimes(2);
                    LocalTime time4 = LocalTime.from(attendance.getLateTime()); // 你的晚勤时间
                    if (time4.isAfter(earlyTime) && time4.isBefore(lateTime)) { // 下午早退
                        attendance.setStatus(2); // 下午早退
                        if (attendance2.getStatus() == 1) { // 上午是迟到
                            attendance.setStatus(3);
                        }
                    }else{
                        attendance.setStatus(0);
                    }
                    Attendance updateAttendance = new Attendance();
                    updateAttendance.setStatus(attendance.getStatus());
                    updateAttendance.setTimes(attendance.getTimes());
                    updateAttendance.setLateTime(attendance.getLateTime());
                    QueryWrapper<Attendance> wrapper1 = new QueryWrapper<>();
                    wrapper1.eq("userid", attendance.getUserid());

                    int update = attendanceMapper.update(updateAttendance, wrapper1);
                    if (update > 0) {
                        return new Result("200", "更新成功", update);
                    } else {
                        return new Result("500", "更新失败", null);
                    }
                }else{
                    attendance.setTimes(1);
                    attendance.setStatus(4);
                    int insert = attendanceMapper.insert(attendance); // 添加
                    if (insert > 0) {
                        return new Result("202", "插入成功", insert);
                    } else {
                        return new Result("500", "插入失败", null);
                    }
                }
            }
        }
        return new Result("444", "空指针", null);
    }

}
